// 04jyhs/admin/home.js
var co = require('../../resource/js/common.js')
var M = require('../../resource/js/md5.js')
var ws = require("../../resource/js/websocket.js")
var app = getApp()
var _this, _RoomID
var interval = null //倒计时函数
Page({
   /*页面的初始数据*/
   data: {
      items: '',
      userInfo: "",
      animation: '',
      audioSrc: '',
   },
   /*生命周期函数--监听页面加载*/
   onLoad: function (opt) {
      _this = this
      console.log(app.Data)
      _this.getUserSetItems()
      _this.setData({
         userInfo: app.Data.userInfo,
      })
      _this.initWS_Login()
   },

   //微信授权登录
   getWxOpenInfo() {
       app.wxLogin(function (res) {
         console.log("【app.wxLogin登录结果】===", res)
          _this.login(res)
      })
   },
   //自动登录用户
   login(obj) {
      co.getShareApi('jyhs/Usermg/userAutoLogin', {
         unionid: obj.unionid,
         openid: obj.openid, //因为换appid，需要更新Wx_Openid
      }, function (res) {
         var D = res.data
         if (res.code == 0) {
            _this.goto(res.data)
            return
         }
      })
   },
   //ws初始化
   initWS_Login() {
      _RoomID = "jyhs_admin"
      ws.initWebsocket(app.Data.userInfo, _RoomID, function (res) {
         console.log('【WS初始化及登录结果】', res)
      })
      //监听WS
      ws.onSocketMsg(function (res) {
         console.log("连接成功", res)
         //企业
         if (res.type == "getBizOrder") {
            if (res.to_user_id == 0){
               _this.playMP3(res.content,2)
            }
         }
      })
   },
   onReady: function () {
      _this.audioCtx = wx.createAudioContext('myAudio')
   },
   // 获取后台列表数据显示
   getUserSetItems() {
      var type = app.Data.userInfo.User_Type
      co.getShareApi('menu/Config/getMenuList', {
         Client: type,
         Client_Type: 'wxapp'
      }, function (res) {
         if (res.code == 0) {
            _this.setData({
               items: res.data
            })
         }
      })
   },
   // 点击进入页面
   toNextUrl: function (e) {
      var e_id = e.currentTarget.id;
      wx.navigateTo({
         url: e_id
      })
   },
   //语音播报
   playMP3: function (data, types) {
      //判断是否是换桌
      co.getJyhsApi('api/Voice/voiceRecognite', {
         vol: 5,
         message: data,
         type: types
      }, function (res) {
         _this.setData({
            audioSrc: res.data
         })
         setTimeout(function () {
            _this.nowPlay()
         }, 100)
      })
   },
   nowPlay: function (res) {
      _this.audioCtx.play()
      console.log("【播放】")
   },
})